import { defineStore } from 'pinia'

export const useHistoryStore = defineStore('history', {
	state: () => {
		return {
			historyList: [],
			nowIndex: 0,
			isHistory: false,
			maxLength: 50
		}
	},
	getters: {
		getHistory: (state) => {
			return state.historyList[state.nowIndex]
		}
	},
	actions: {
		addHistory(data: any) {
			this.historyList.unshift(JSON.parse(JSON.stringify(data)))
			if (this.historyList.length > this.maxLength)
				this.historyList.length = this.maxLength
			this.nowIndex = 0
		},
		setIndex(index: number) {
			this.isHistory = true
			this.nowIndex = this.nowIndex + index
			if (this.nowIndex >= this.maxLength) {
				this.nowIndex = this.maxLength - 1
			} else if (this.nowIndex < 0) {
				this.nowIndex = 0
			}
		},
		setIsHistory(isHistory: boolean) {
			this.isHistory = isHistory
		}
	}
})
